import requests
import random

from bs4 import BeautifulSoup


class Site:
    @staticmethod
    def get_page_elements(text, tag, href=False, attrs=None):
        return BeautifulSoup(str(text), 'lxml').find_all(tag, href=href, attrs=attrs)

    @staticmethod
    def get_page(type_of_work, page_number):
        URL = f'https://animego.org/{type_of_work}?sort=a.createdAt&direction=desc&type={type_of_work}s&page={page_number}'
        return requests.get(URL)


class Parser:
    def __init__(self):
        self.counter = {'anime': 1, 'manga': 1}

    def get_work(self, type_of_work):
        page = Site.get_page(type_of_work, self.counter[type_of_work])
        links = random.choice(
            Site.get_page_elements(
                Site.get_page_elements(                                                                  
                    page.text, 'div', 
                    attrs={'class': 'h5 font-weight-normal mb-1'}    
                ), 'a', href=True
            ),
        )
        self.counter[type_of_work] += 1
        return f"{links.text}:\n{links['href']}"
